//
//  MCMainTabBarController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "MCMainTabBarController.h"
#import "HomeViewController.h"
#import "FBTradeViewController.h"
#import "LeverageTradeViewController.h"
#import "MineViewController.h"
#import "MCMainNavigationController.h"
#import "KKWebViewController.h"


@interface WKTabBar ()
@end
@implementation WKTabBar

- (void)layoutSubviews {
    [super layoutSubviews];

    /**** 设置所有UITabBarButton的frame ****/
    // 按钮的尺寸
    CGFloat buttonW = self.frame.size.width / 4;
    CGFloat buttonH = self.frame.size.height;
    CGFloat buttonY = 0;
    // 按钮索引
    int buttonIndex = 0;

    for (UIView *subview in self.subviews) {
        // 过滤掉非UITabBarButton
        //   if (![@"UITabBarButton" isEqualToString:NSStringFromClass(subview.class)]) continue;
        if (subview.class != NSClassFromString(@"UITabBarButton")) continue;

        // 设置frame
        CGFloat buttonX = buttonIndex * buttonW;
        
        subview.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH-(kTabBarHeight-49));
        // 增加索引
        buttonIndex++;
    }

    
}

@end


@interface MCMainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MCMainTabBarController

+(void)load{
    
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:MC_THEME_COLOR,NSFontAttributeName:[UIFont boldSystemFontOfSize:12]} forState:UIControlStateSelected];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:MC_ColorWithAlpha(125, 125, 134, 1),NSFontAttributeName:[UIFont boldSystemFontOfSize:12]} forState:UIControlStateNormal];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    [self setChildViewController];
    
}

- (void)setChildViewController{
    
    
    [self setChildViewController:[[HomeViewController alloc] init] title:@"首页" normalImage:[UIImage originalImageFromImageNamed:@"tabbar-home-normal"] selectedImage:[UIImage originalImageFromImageNamed:@"tabbar-home-highlight"]];
   
    [self setChildViewController:[[LeverageTradeViewController alloc] init] title:@"杠杆交易" normalImage:[UIImage originalImageFromImageNamed:@"tabbar-prize-normal"] selectedImage:[UIImage originalImageFromImageNamed:@"tabbar-prize-highlight"]];
    
    [self setChildViewController:[[FBTradeViewController alloc] init] title:@"法币交易" normalImage:[UIImage originalImageFromImageNamed:@"tabbar-lottery-normal"] selectedImage:[UIImage originalImageFromImageNamed:@"tabbar-lottery-highlight"]];

    [self setChildViewController:[[MineViewController alloc] init] title:@"我的" normalImage:[UIImage originalImageFromImageNamed:@"tabbar-my-normal"] selectedImage:[UIImage originalImageFromImageNamed:@"tabbar-my-highlight"]];
    [self setValue:[[WKTabBar alloc] init] forKeyPath:@"tabBar"];

}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    //是第三个根视图且本次点击跟上次一样，就不切换
//    if (viewController == self.viewControllers[3]) {
//        //点击中间tabbarItem，不切换，让当前页面跳转
//        tabbarThdSelected = @"tabbarThdSelected";
//    }else{
//        tabbarThdSelected = @"";
//    }
    return YES;
}


- (void)setChildViewController:(UIViewController *)viewController title:(NSString *)title normalImage:
(UIImage *)image selectedImage:(UIImage *)selectedImage{
    
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selectedImage;
    MCMainNavigationController *navi = [[MCMainNavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:navi];
    
    
}



@end

